package com.neusoft.resturant.controller;

import com.neusoft.resturant.entity.Student;
import com.neusoft.resturant.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.swing.*;
import java.util.List;

/**
 * @author: Daniel
 * @date: 2023-10-25 13:58
 **/
@RestController
@RequestMapping("/api/student")
public class StudentController {

    @Autowired
    private IStudentService studentService;

    // http://localhost:8080/api/student/detail?sno=202200001&name=张三&sex=男
    @GetMapping("/detail")
    public Student getDetail(HttpServletRequest request) {
        String sno = request.getParameter("sno");
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        System.out.println("学生学号：" + sno);
        System.out.println("学生姓名：" + name);
        // 1 掉service service 调dao，
        // 2 dao根据id去DB查找
        Student student = new Student();
        student.setName(name);
        student.setSno(sno);
        student.setSex(sex);
        return student;
    }

    // http://localhost:8080/api/student/detail1?sno=202200001&name=张三&sex=男
    @GetMapping("/detail1")
    public Student detail(String sno, String name, String sex) {
        System.out.println("学生学号：" + sno);
        System.out.println("学生姓名：" + name);
        // 1 掉service service 调dao，
        // 2 dao根据id去DB查找
        Student student = new Student();
        student.setName(name);
        student.setSno(sno);
        student.setSex(sex);
        return student;
    }

    // http://localhost:8080/api/student/detail2?sno=202200001&name=张三&sex=男
    @GetMapping("/detail2")
    public Student detail2(Student student) {
        System.out.println("detail2");
        // 1 掉service service 调dao，
        // 2 dao根据id去DB查找
        return student;
    }

    // http://localhost:8080/api/student/detail3
    @RequestMapping(value = "/detail3", method = RequestMethod.GET)
    public void getUserName(@RequestParam(value = "name",
            required = false, defaultValue = "lisi") String name) {
        System.out.println("stuName=" + name);
    }

    //    http://localhost:8080/api/student/1
    @GetMapping("/{id}")
    public Student getInfo(@PathVariable(value = "id") Long stuId) {
        System.out.println("id=" + stuId);
        Student student = studentService.selectStudentById(stuId);
        return student;
    }

    //    http://localhost:8080/api/student/list?sno=xx
    //    http://localhost:8080/api/student/list?name=xx
    //    http://localhost:8080/api/student/list?name=xx&sno=ooo
    @GetMapping("/list")
    public Student list(Student student) {
        System.out.println("student=" + student);
        return student;

    }

    //RequestBody就会把json转化为对象
    // http://localhost:8080/api/student  POST
    @PostMapping
    public Student add(@RequestBody Student student) {
        System.out.println("新增学生信息");
        System.out.println("student=" + student);
        return student;
    }

    //RequestBody就会把json转化为对象
    // http://localhost:8080/api/student  PUT
    @PutMapping
    public Student update(@RequestBody Student student) {
        System.out.println("修改学生信息");
        System.out.println("student=" + student);
        return student;
    }

    //    http://localhost:8080/api/student/10
    @DeleteMapping("/{id}")
    public void delete(@PathVariable(value = "id") Long id) {
        System.out.println("删除学生信息");
        System.out.println("id=" + id);
    }


    //RequestBody就会把json转化为对象
    // http://localhost:8080/api/student/batch  POST
    @PostMapping("/batch")
    public Student[] batchAdd(@RequestBody Student[] studentArr) {
        System.out.println("批量新增学生信息");
        for (Student student : studentArr) {
            System.out.println(student);
        }
        return studentArr;
    }

    //RequestBody就会把json转化为对象
    // http://localhost:8080/api/student/batch1  POST
    @PostMapping("/batch1")
    public List<Student> batchAdd(@RequestBody List<Student> studentArr) {
        System.out.println("批量新增学生信息-列表");
        for (Student student : studentArr) {
            System.out.println(student);
        }
        return studentArr;
    }


}